package com.hongyu.smart.village.ui.fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.hongyu.smart.village.R;
import com.hongyu.smart.village.config.Constant;
import com.hongyu.smart.village.event.Event;
import com.hongyu.smart.village.listener.OnItemClickListener;
import com.hongyu.smart.village.model.HomeMenu;
import com.hongyu.smart.village.ui.activity.BenefitActivity;
import com.hongyu.smart.village.ui.activity.ConvenientActivity;
import com.hongyu.smart.village.ui.activity.InteractActivity;
import com.hongyu.smart.village.ui.activity.NoticeActivity;
import com.hongyu.smart.village.ui.adapter.HomeMenuAdapter;
import com.hongyu.smart.village.ui.base.BaseFragment;
import com.hongyu.smart.village.ui.widget.banner.BannerAdapter;
import com.hongyu.smart.village.ui.widget.banner.BannerView;
import com.hongyu.smart.village.ui.widget.banner.BannerViewPager;
import com.hongyu.smart.village.ui.widget.banner.transformer.CubeOutTransformer;
import com.superluo.textbannerlibrary.ITextBannerItemClickListener;
import com.superluo.textbannerlibrary.TextBannerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class HomeFragment extends BaseFragment {
    @BindView(R.id.banner)
    BannerView bannerView;
    @BindView(R.id.recycle_view)
    RecyclerView recyclerView;
    @BindView(R.id.news_recycle_view)
    RecyclerView newsRecyclerView;
    @BindView(R.id.tv_banner)
    TextBannerView textBannerView;

    private BannerViewPager mBannerViewPager;
    List<HomeMenu> homeMenuList=new ArrayList<>();
    HomeMenuAdapter menuAdapter;
    List<String> promoList= new ArrayList<>();
    private final String[] mUrls = {"https://pics4.baidu.com/feed/3812b31bb051f819979b8581d0ee77e72f73e727.png?token=081a892350d5ae1f023b40045c981cfd",
            "https://pics3.baidu.com/feed/9358d109b3de9c82779c706464dbbd001bd843c0.jpeg?token=5fd819b0f2f70a025397fc761d5d1b78",
            "https://pics4.baidu.com/feed/f9198618367adab47c5ba53b9b8e8e168601e4a7.png?token=25fd2f175bec6555a8bd0def4fb98dbf"};
    @Override
    protected View getContentView(LayoutInflater inflater, @Nullable ViewGroup container) {
        return inflater.inflate(R.layout.fragment_home, container, false);
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {
        setupBannerView();
        setupTextBannerView();
        menuAdapter=new HomeMenuAdapter(this.getContext(),homeMenuList);
        recyclerView.setLayoutManager(new GridLayoutManager(this.getContext(),4));
        recyclerView.setAdapter(menuAdapter);
        homeMenuList.clear();
        int  index=0;
        for (String name: Constant.homeMenuName){
            HomeMenu menu=new HomeMenu();
            menu.setName(name);
            menu.setImageres(Constant.homeMenuIcon[index]);
            index++;
            homeMenuList.add(menu);
        }
        menuAdapter.notifyDataSetChanged();
        menuAdapter.setmOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                Toast.makeText(getActivity().getApplicationContext(),""+position,Toast.LENGTH_SHORT).show();
                switch (position){
                    case 0:
                        sendIntent(NoticeActivity.class);
                        break;
                    case 1:

                        break;
                    case 2:

                        break;
                    case 3:

                        break;
                    case 4:
                        sendIntent(InteractActivity.class);
                        break;
                    case 5:
                        sendIntent(ConvenientActivity.class);
                        break;
                    case 6:
                        sendIntent(BenefitActivity.class);
                        break;
                }
            }
        });
    }

    private void setupTextBannerView() {
        for(int i=0;i<Constant.textBannerTitles.length;i++){
            promoList.add(Constant.textBannerTitles[i]);
        }
        textBannerView.setDatas(promoList);
        textBannerView.setItemOnClickListener(new ITextBannerItemClickListener() {
            @Override
            public void onItemClick(String data, int position) {
                //获取点击的数据，然后需要做的事情，根据情况而定，我这里是实现跳转


            }
        });


    }

    private void setupBannerView() {
        mBannerViewPager = bannerView.getBannerViewPager();
        mBannerViewPager.setPageTransformer(false, new CubeOutTransformer());
        bannerView.setAdapter(mBannerAdapter);
        bannerView.setScrollerDuration(1288);
        bannerView.startLoop();


        recyclerView.setLayoutManager(new GridLayoutManager(this.getContext(),3));
        recyclerView.setAdapter(menuAdapter);

    }
    private final BannerAdapter mBannerAdapter = new BannerAdapter() {
        @Override
        public View getView(int position, View convertView) {
            ImageView bannerIv;
            if (convertView == null) {
                bannerIv = new ImageView(getActivity());
                bannerIv.setScaleType(ImageView.ScaleType.CENTER_CROP);
            } else {
                bannerIv = (ImageView) convertView;

            }
            Glide.with(getContext()).load(mUrls[position]).into(bannerIv);
            return bannerIv;
        }

        @Override
        public int getCount() {
            return mUrls.length;
        }
    };

    @Override
    protected void onEventProcess(Event event) {

    }

    // 4.为了出现重影，还得在onResume和onStop中设置
    @Override
    public void onResume() {
        super.onResume();
        textBannerView.startViewAnimator(); //1.防止出现重影
    }

    @Override
    public void onStop() {
        super.onStop();
        textBannerView.stopViewAnimator();  //2.防止出现重影
    }

}
